package cn.tank.rabbitwork.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;


@Configuration
public class RabbitConfig {

    /**
     * 订单队列
     */
    @Value("${rabbitmq.queue.q-order}")
    public String orderQueue;
    /**
     * ttl过期队列
     */
    @Value("${rabbitmq.queue.q-pay-ttl-waiting}")
    public String payWaitingQueue;
    /**
     * dead死信队列
     */
    @Value("${rabbitmq.queue.q-pay-dead}")
    public String payDeadQueue;

    /**
     * 订单交换器
     */
    @Value("${rabbitmq.queue.ex-order}")
    public String orderEx;
    /**
     * ttl过期交换器
     */
    @Value("${rabbitmq.queue.ex-pay-ttl-waiting}")
    public String payWaitingEx;
    /**
     * dlx死信交换器
     */
    @Value("${rabbitmq.queue.ex-pay-dead}")
    public String payDeadEx;


    /**
     *死信队列绑定key
     */
    @Value("${rabbitmq.queue.rk-order}")
    public String orderRoutingKey;
    /**
     *ttl队列绑定key
     */
    @Value("${rabbitmq.queue.rk-pay-ttl-waiting}")
    public String payWaitingRoutingKey;
    /**
     *死信队列绑定key
     */
    @Value("${rabbitmq.queue.rk-pay-dead}")
    public String payDeadRoutingKey;

    @Value("${rabbitmq.queue.waiting-time}")
    public Integer expireTime;

    @Bean
    public Queue queueWaiting() {
        HashMap<String, Object> props = new HashMap<>();
        //消息过期时间
        props.put("x-message-ttl", expireTime * 1000);
        props.put("x-dead-letter-exchange",payDeadEx);
        props.put("x-dead-letter-routing-key",payDeadRoutingKey);
        Queue queue = new Queue(payWaitingQueue, false, false, false, props);
        return queue;
    }
    @Bean
    public Queue queueDead(){
        Queue queue = new Queue(payDeadQueue, true, false, false);
        return queue;
    }
    @Bean
    public Queue orderQueue(){
        Queue queue = new Queue(orderQueue, true, false, false);
        return queue;
    }

    @Bean
    public Exchange exchangeWaiting() {
        DirectExchange directExchange = new DirectExchange(payWaitingEx, false, false);
        return directExchange;
    }
    @Bean
    public Exchange exchangeDead(){
        DirectExchange directExchange = new DirectExchange(payDeadEx, true, false);
        return directExchange;
    }
    @Bean
    public Exchange exchangeOrder(){
        DirectExchange directExchange = new DirectExchange(orderEx, true, false);
        return directExchange;
    }

    @Bean
    public Binding bindingWaiting(@Qualifier("exchangeWaiting") Exchange exchange,
                                  @Qualifier("queueWaiting") Queue queue) {
        return BindingBuilder.bind(queue).to(exchange).with(payWaitingRoutingKey).noargs();
    }
    @Bean
    public Binding bindingDead(@Qualifier("exchangeDead") Exchange exchange,
                               @Qualifier("queueDead") Queue queue){
        return BindingBuilder.bind(queue).to(exchange).with(payDeadRoutingKey).noargs();
    }
    @Bean
    public Binding bindingPay(@Qualifier("exchangeOrder") Exchange exchange,
                               @Qualifier("orderQueue") Queue queue){
        return BindingBuilder.bind(queue).to(exchange).with(orderRoutingKey).noargs();
    }
}
